#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        if (n == 0) return true;
        int m = flowerbed.size();
        if (m == 1) return n + flowerbed[0] <= 1;
        int plants = 0;
        for (int i=0; i<m; ++i) {
            if (i == 0) {
                if (!flowerbed[0] && !flowerbed[1]) {
                    flowerbed[0] = 1;
                    plants++;
                }
            } else if (i == m-1) {
                if (!flowerbed[i] && !flowerbed[i-1]) {
                    flowerbed[i] = 1;
                    plants++;
                }
            } else if (!flowerbed[i] && !flowerbed[i-1] && !flowerbed[i+1]) {
                flowerbed[i] = 1;
                plants++;
            }
        }
        return plants >= n;
    }
};
